<template>
	<view class="content">
		<button @click="addUser">添加用户数据</button>
		<button @click="selectUser">查询数据</button>
		<button @click="onTransactional">事务测试，需要看着接口来该数据</button>
	</view>
</template>

<script lang="ts">
	import { User } from '@/pages/example/model/User';
	import { UserServiceImpl } from '@/pages/example/service/UserServiceImpl';
	const userService = new UserServiceImpl();
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {
			// this.addUser()
		},
		methods: {
			// 添加数据
			async addUser() {
				try {
					// 插入数据
					const user = new User();
					user.name = '张三';
					user.age = Math.floor(Math.random() * (35 - 18 + 1)) + 18;
					user.price = 235
					await userService.insertUser(user);

					console.log("数据添加成功");

					// 根据主键查询数据
					const userById = await userService.getUserById(1);
					console.log('根据主键查询数据：', userById);

					// 查询所有数据
					const userList = await userService.getUsers();
					console.log('查询所有数据：', userList);

					// 根据条件查询数据
					const userByCondition = await userService.getUsersByCondition("age > 18");
					console.log('根据条件查询数据：', userByCondition);

					// 分页查询数据
					const page = 3;
					const size = 10;
					const userPage = await userService.getUsersByPage(page, size, "age > 18", "age ASC");
					console.log('分页查询数据：', userPage);
					console.log(userById);
					// 更新数据
					userById.name = '李四';
					await userService.updateUser(userById);

					// 根据主键删除数据
					// await userService.deleteUserById(3);

					// 根据条件删除数据
					// await userService.deleteUserByCondition("age < 30");
				} catch (error) {
					console.error('An error occurred during database operation:', error);
				}
			},

			// 查询数据
			async selectUser() {
				try {
					const user = await userService.getUsersByCondition("age > 30 and (id <=5 and id >= 2)")
					console.log(user);

					const userList = await userService.getUsers();
					console.log('查询所有数据：', userList);
				} catch (error) {
					console.error('An error occurred during database operation:', error);
				}
			},
			onTransactional() {
				console.log("---------------*");
				const user = new User();
				user.name = '事务名称';
				user.age = Math.floor(Math.random() * (35 - 18 + 1)) + 18;
				user.price = 254
				userService.transactionalTest(user)
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>